package com.elves.auth.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.elves.auth.entity.material.Material;
import com.elves.auth.entity.material.MaterialJpa;
import com.elves.auth.entity.material.MaterialType;
import com.elves.auth.entity.material.MaterialTypeJpa;
import com.elves.auth.entity.supplier.Supplier;
import com.elves.auth.entity.supplier.SupplierJpa;
import jakarta.annotation.Resource;
import jakarta.persistence.Transient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;

@Component
@Slf4j
public class MaterialService {
    @Resource
    SupplierService supplierService;
    @Resource
    MaterialTypeJpa materialTypeJpa;
    @Resource
    SupplierJpa supplierJpa;
    @Resource
    MaterialJpa materialJpa;

    public void saveMaterialTypes(MultipartFile file) {
        List<Object> materialTypeData;
        try (InputStream inputStream = file.getInputStream()) {
            materialTypeData = EasyExcel.read(inputStream)
                    .sheet("设备类别")
                    .doReadSync();
            log.info("读取到{}条设备类别数据", materialTypeData.size());

            for (int i = 0; i < materialTypeData.size(); i++) {
                Object obj = materialTypeData.get(i);
                log.info("{} excel supplier {} data:{}", i, obj.getClass(), obj);
                MaterialType materialType = MaterialType.mapMaterialType(CommonService.rowToMap(obj));
                if (materialType != null) {
                    materialTypeJpa.save(materialType);
                } else {
                    log.error("解析错误：{}:{}", i, obj);
                }
            }

        } catch (IOException e) {
            log.error("读取Excel文件设备类别失败", e);
            throw new RuntimeException("读取Excel文件设备类别失败", e);
        }
    }


    public void saveMaterials(MultipartFile file ){
        List<Object> materialTypeData;
        try (InputStream inputStream = file.getInputStream()) {
            materialTypeData = EasyExcel.read(inputStream)
                    .sheet("设备价格")
                    .doReadSync();
            log.info("读取到{}条设备价格数据", materialTypeData.size());
            for (int i = 0; i < materialTypeData.size(); i++) {
                Object obj = materialTypeData.get(i);
                log.info("{} excel supplier {} data:{}", i, obj.getClass(), obj);
                Material material = Material.mapToMaterial(CommonService.rowToMap(obj),  materialTypeJpa,   supplierJpa);
                if (material != null) {
                    log.info("保存物料信息{}:{}", i, material);
                    materialJpa.save(material);
                } else {
                    log.error("解析错误：{}:{}", i, obj);
                }
            }
        } catch (IOException e) {
            log.error("读取Excel文件设备类别失败", e);
            throw new RuntimeException("读取Excel文件设备类别失败", e);
        }
    }


    public Boolean importMaterial(MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            log.info("materialUpload file is empty");
            log.warn("上传文件不能为空");
            return false;
        }

        // 0、 查看excel的所有sheet页
        try (InputStream inputStream = file.getInputStream()) {
            ExcelReader excelReader = EasyExcel.read(inputStream).build();
            log.info("excel有{}个sheet页", excelReader.excelExecutor().sheetList());
        } catch (Exception e) {
            log.error("excel读取失败", e);
            throw new RuntimeException("excel读取失败");
        }
        // 1、插入设备物料类型
        this.saveMaterialTypes(file);
        // 2、插入供应商
        supplierService.saveSuppliers(file);
        // 3、插入物料信息
        this.saveMaterials(file);
        log.info("导入完成");
        // 返回值待检查完善
        log.warn("待检查完善返回值");
        return true;
    }


}
